package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.excelEntity.WillingnessSurveyExcel;
import com.xmy.cultivate.entity.views.WillingnessSurveyMap;
import com.xmy.cultivate.mapper.WillingnessSurveyMapper;
import com.xmy.cultivate.service.IWillingnessSurveyService;
import com.xmy.cultivate.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class WillingnessSurveyServiceImpl implements IWillingnessSurveyService {
    @Autowired
    WillingnessSurveyMapper willingnessSurveyMapper;

    @Override
    public PageResult<WillingnessSurveyExcel> getWillingnessSurvey(Map<String, Object> reMap) {
        int pageNum = reMap.get("pageNum") == null || reMap.get("pageNum").toString().isEmpty() ? 1 : Integer.parseInt(reMap.get("pageNum").toString());
        int pageSize = reMap.get("pageSize") == null || reMap.get("pageSize").toString().isEmpty() ? 10 : Integer.parseInt(reMap.get("pageSize").toString());

        IPage<WillingnessSurveyExcel> iPage = new Page<>(pageNum,pageSize);

        String school_id = reMap.get("schoolId") != null && !reMap.get("schoolId").toString().isEmpty() ? reMap.get("schoolId").toString() : null;
        String teacherId = reMap.get("teacherId") != null && !reMap.get("teacherId").toString().isEmpty() ? reMap.get("teacherId").toString() : null;

        String status = reMap.get("status") != null && !reMap.get("status").toString().isEmpty() ? reMap.get("status").toString() : null;
        String subjectsId = reMap.get("subjectsId") != null && !reMap.get("subjectsId").toString().isEmpty() ? reMap.get("subjectsId").toString() : null;

        String topicId = reMap.get("topicId") != null && !reMap.get("topicId").toString().isEmpty() ? reMap.get("topicId").toString() : null;

        String yearPart = reMap.get("yearPart") != null && !reMap.get("yearPart").toString().isEmpty() ? reMap.get("yearPart").toString() : null;
        String quarter = reMap.get("quarter") != null && !reMap.get("quarter").toString().isEmpty() ? reMap.get("quarter").toString() : null;

        String yearClassId = reMap.get("yearClassId") != null && !reMap.get("yearClassId").toString().isEmpty() ? reMap.get("yearClassId").toString() : null;
        String gradeId = reMap.get("gradeId") != null && !reMap.get("gradeId").toString().isEmpty() ? reMap.get("gradeId").toString() : null;

        String studentName = reMap.get("studentName") != null && !reMap.get("studentName").toString().isEmpty() ? reMap.get("studentName").toString() : null;
        String studentId = reMap.get("studentId") != null && !reMap.get("studentId").toString().isEmpty() ? reMap.get("studentId").toString() : null;

        String lessonNum = reMap.get("lessonNum") != null && !reMap.get("lessonNum").toString().isEmpty() ? reMap.get("lessonNum").toString() : null;
        String lessonType = reMap.get("lessonType") != null && !reMap.get("lessonType").toString().isEmpty() ? reMap.get("lessonType").toString() : null;


        QueryWrapper<WillingnessSurveyExcel> willingnessSurveyQueryWrapper = new QueryWrapper<>();
        willingnessSurveyQueryWrapper.eq("1",1);
        String field = "*," +
                "(" +
                "SELECT" +
                " GROUP_CONCAT( CONCAT( t_name, '-', o_name ) SEPARATOR '    ' ) " +
                " FROM " +
                "(" +
                " SELECT " +
                "t.`name` AS t_name," +
                "o.`name` AS o_name " +
                " FROM " +
                " willingness_survey AS s " +
                " INNER JOIN willingness_survey_option AS t ON t.type = s.type " +
                " INNER JOIN willingness_survey_option_parameter AS o ON o.type = t.type " +
                " AND o.state = s.state " +
                " WHERE " +
                " s.student_id = willingness_survey.student_id " +
                " AND s.add_time = willingness_survey.add_time " +
                ") AS wsop " +
                ") AS surveyResultName ";
//        willingnessSurveyQueryWrapper.select(field);
        if(StringUtils.isNotBlank(school_id)){
            willingnessSurveyQueryWrapper.eq(StringUtils.isNotBlank(school_id),"school_id",school_id);
        }
        willingnessSurveyQueryWrapper.eq(StringUtils.isNotBlank(subjectsId),"subjects_id",subjectsId);
        willingnessSurveyQueryWrapper.eq(StringUtils.isNotBlank(teacherId),"teacher_id",teacherId);
        willingnessSurveyQueryWrapper.eq(StringUtils.isNotBlank(lessonType),"lesson_type",lessonType);

        willingnessSurveyQueryWrapper.eq(StringUtils.isNotBlank(yearPart),"year_part",yearPart);
        willingnessSurveyQueryWrapper.eq(StringUtils.isNotBlank(quarter),"quarter_num",quarter);

        willingnessSurveyQueryWrapper.eq(StringUtils.isNotBlank(yearClassId),"year_class_id",yearClassId);
        willingnessSurveyQueryWrapper.eq(StringUtils.isNotBlank(gradeId),"grade_id",gradeId);

        willingnessSurveyQueryWrapper.like(StringUtils.isNotBlank(studentName), "student_name", studentName);

        willingnessSurveyQueryWrapper.like(StringUtils.isNotBlank(studentId),"student_id",studentId);

        willingnessSurveyQueryWrapper.like(StringUtils.isNotBlank(lessonNum),"lesson_num",lessonNum);

        if(StringUtils.isNotBlank(status)){
            willingnessSurveyQueryWrapper.eq("status",status);
        }

        if (topicId != null && !topicId.isEmpty() && !topicId.equals("[]"))  {
            List<WillingnessSurveyMap> willingnessSurveyMaps = new ArrayList<>();
            String[] topicStr = topicId.split(",");
            for (String topicIdStr : topicStr) {
                String[] topicIdArr = topicIdStr.split("_");
                WillingnessSurveyMap willingnessSurveyMap = new WillingnessSurveyMap();
                willingnessSurveyMap.setId(Integer.valueOf(topicIdArr[0]));
                willingnessSurveyMap.setAnswer(Integer.valueOf(topicIdArr[1]));
                willingnessSurveyMaps.add(willingnessSurveyMap);
            }
//            List<WillingnessSurveyMap> willingnessSurveyMaps = JSON.parseArray(JSON.toJSONString(reMap.get("topicId")), WillingnessSurveyMap.class);

            if (!willingnessSurveyMaps.isEmpty()) {
                willingnessSurveyQueryWrapper.and(q -> {
                    for (WillingnessSurveyMap willingnessSurveyMap : willingnessSurveyMaps) {
                        q.or(o -> o.eq("type", willingnessSurveyMap.getId()).eq("state", willingnessSurveyMap.getAnswer()));
                    }
                });
                willingnessSurveyQueryWrapper.having("COUNT(type) > " + (willingnessSurveyMaps.size() - 1));
            }
        }

        willingnessSurveyQueryWrapper.groupBy("student_id,add_time,subjects_id");

        willingnessSurveyQueryWrapper.orderByDesc("id");

        willingnessSurveyMapper.getWillingnessSurvey(iPage, willingnessSurveyQueryWrapper, field);
        return new PageResult<>(iPage.getRecords(), iPage.getTotal(), iPage.getPages());
    }
}
